#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    connect(&imgMap,SIGNAL(updateProgress()), this, SLOT(showProgress()));
    connect(&imgMap,SIGNAL(complete()), this, SLOT(readMapComplete()));

    ui->pushButton->setVisible(false);
    ui->pushButton_2->setVisible(false);
    ui->pushButton_3->setVisible(false);
    ui->pushButton_4->setVisible(false);
    ui->progress1->setVisible(false);
    ui->imgLabel->setVisible(false);

    timer = new QTimer(this);
    timer1 = new QTimer(this);
    timer2 = new QTimer(this);
    timer3 = new QTimer(this);

    img2=QImage("d:\\pic2.png");
    img1=QImage("d:\\pic1.png");

    connect(timer1, SIGNAL(timeout()), this, SLOT(showBack()));
    connect(timer, SIGNAL(timeout()), this, SLOT(showMove()));

    connect(timer2, SIGNAL(timeout()), this, SLOT(showMove1()));
    connect(timer3, SIGNAL(timeout()), this, SLOT(showBack1()));

    resizeUi.init(405,525);
    resizeUi.addInfo(ui->info);
    resizeUi.addInfo(ui->btn3);
    resizeUi.addInfo(ui->btn4);
    resizeUi.addInfo(ui->btn5);
    resizeUi.addInfo(ui->btn6);
    resizeUi.addInfo(ui->btn7);
    resizeUi.addInfo(ui->progress1);
    resizeUi.addInfo(ui->pushButton);
    resizeUi.addInfo(ui->pushButton_2);
    resizeUi.addInfo(ui->pushButton_3);
    resizeUi.addInfo(ui->pushButton_4);
    resizeUi.addInfo(ui->imgLabel);

    QDir::setCurrent(tr("/mnt/sdcard"));
}

MainWindow::~MainWindow()
{
    imgMap.exit();
    delete ui;
}

void MainWindow::readMapComplete()
{
    ui->progress1->setVisible(false);
    ui->btn3->setVisible(false);

    ui->pushButton->setVisible(true);
    ui->pushButton_2->setVisible(true);
    ui->pushButton_3->setVisible(true);
    ui->pushButton_4->setVisible(true);

}

void MainWindow::showProgress()
{
    ui->progress1->setValue((int)(imgMap.progress*100));
    img1=imgMap.img1;
    showPic(img1.width(),img1.height());
}

void MainWindow::showPic(int width,int height)
{
    int show_width=ui->imgLabel->width();
    int show_height=ui->imgLabel->height();

    double r1=1.0*show_width/width;
    double r2=1.0*show_height/height;

    double r=min(r1,r2);

    QImage temp=img1.scaled(width*r, height*r, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
//    ui->imgLabel->resize(temp.width(),temp.height());
    ui->imgLabel->setPixmap(QPixmap::fromImage(temp));
}

//void MainWindow::paintEvent(QPaintEvent *)
//{
//    if(!startReadMap)
//        return ;
//    QImage temp=img1.scaled(img1_width, img1_height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
//    int w=this->width();
//    int h=ui->pushButton->y()-10;
//    QRectF target(0, 0, w, h);
//    QRectF src(0, 0, temp.width(), temp.height());
//    QPainter painter1(this);
//    painter1.drawImage(target, temp, src);
//}

void MainWindow::resizeEvent(QResizeEvent *event)
{
    resizeUi.resizes(this->width(),this->height());
//    int h=ui->pushButton->height();
//    int w=ui->pushButton->width();
//    int x=ui->pushButton->x();
//    int y=this->height()-h-40;
//    ui->pushButton->setGeometry(x,y,w,h);
//    ui->pushButton_2->setGeometry(ui->pushButton_2->x(),y,w,h);
//    ui->pushButton_3->setGeometry(ui->pushButton_3->x(),y,w,h);
//    ui->pushButton_4->setGeometry(ui->pushButton_4->x(),y,w,h);
//    this->update();
}


void MainWindow::showMove()
{
    if(showTimes>TotalTimes)
    {
        timer->stop();
        return ;
    }
    double scale=showTimes*1.0/TotalTimes;

    int w=img1.width();
    int h=img1.height();

    uchar *data=img1.bits();
    memset(data,0,img1.byteCount());

    for(int i=0;i<w;i++)
    {
        for(int j=0;j<h;j++)
        {
            int x=(imgMap.map[i][j].ex-i)*scale+i;
            int y=(imgMap.map[i][j].ey-j)*scale+j;


            int r=(imgMap.map[i][j].er-imgMap.map[i][j].sr)*scale+imgMap.map[i][j].sr;
            int g=(imgMap.map[i][j].eg-imgMap.map[i][j].sg)*scale+imgMap.map[i][j].sg;
            int b=(imgMap.map[i][j].eb-imgMap.map[i][j].sb)*scale+imgMap.map[i][j].sb;

            img1.setPixel(x,y,qRgb(r,g,b));
        }
    }

    showTimes++;
    showPic(img2.width(),img2.height());
}


void MainWindow::showMove1()
{
    if(showTimes>TotalTimes)
    {
        timer2->stop();
        return ;
    }

    int w=img1.width();
    int h=img1.height();
    uchar *data=img1.bits();
    memset(data,0,img1.byteCount());

    if(showTimes<TotalTimes/3)
    {
        double scale=showTimes*1.0/TotalTimes*3;
        int t=w*scale;
        for(int i=0;i<w;i++)
        {
            for(int j=0;j<h;j++)
            {
                int r=(imgMap.map[i][j].er);
                int g=(imgMap.map[i][j].eg);
                int b=(imgMap.map[i][j].eb);
                if(i>t)
                {
                    r=(imgMap.map[i][j].sr);
                    g=(imgMap.map[i][j].sg);
                    b=(imgMap.map[i][j].sb);
                }
                img1.setPixel(i,j,qRgb(r,g,b));
            }
        }
    }
    else
    {
        double scale=(showTimes*1.0-TotalTimes/3.0)/(TotalTimes*2/3.0);
        if(scale>0.99)
            scale=1.0;
        for(int i=0;i<w;i++)
        {
            for(int j=0;j<h;j++)
            {
                int x=(imgMap.map[i][j].ex-i)*scale+i;
                int y=(imgMap.map[i][j].ey-j)*scale+j;
                int r=(imgMap.map[i][j].er);
                int g=(imgMap.map[i][j].eg);
                int b=(imgMap.map[i][j].eb);
                img1.setPixel(x,y,qRgb(r,g,b));
            }
        }
    }
    showTimes++;
    showPic(img2.width(),img2.height());
}

void MainWindow::showBack()
{
    if(showTimes<0)
    {
        timer1->stop();
        return ;
    }
    double scale=showTimes*1.0/TotalTimes;

    int w=img1.width();
    int h=img1.height();
    uchar *data=img1.bits();
    memset(data,255,img1.byteCount());
    for(int i=0;i<w;i++)
    {
        for(int j=0;j<h;j++)
        {
            int x=(imgMap.map[i][j].ex-i)*scale+i;
            int y=(imgMap.map[i][j].ey-j)*scale+j;

            int r=(imgMap.map[i][j].er-imgMap.map[i][j].sr)*scale+imgMap.map[i][j].sr;
            int g=(imgMap.map[i][j].eg-imgMap.map[i][j].sg)*scale+imgMap.map[i][j].sg;
            int b=(imgMap.map[i][j].eb-imgMap.map[i][j].sb)*scale+imgMap.map[i][j].sb;

            img1.setPixel(x,y,qRgb(r,g,b));
        }
    }

    showTimes--;
    showPic(img1.width(),img1.height());
}

void MainWindow::showBack1()
{
    if(showTimes<0)
    {
        timer3->stop();
        return ;
    }

    int w=img1.width();
    int h=img1.height();
    uchar *data=img1.bits();
    memset(data,0,img1.byteCount());

    if(showTimes<TotalTimes/3)
    {
        double scale=showTimes*1.0/TotalTimes*3;
        int t=w*scale;
        for(int i=0;i<w;i++)
        {
            for(int j=0;j<h;j++)
            {
                int r=(imgMap.map[i][j].er);
                int g=(imgMap.map[i][j].eg);
                int b=(imgMap.map[i][j].eb);
                if(i>t)
                {
                    r=(imgMap.map[i][j].sr);
                    g=(imgMap.map[i][j].sg);
                    b=(imgMap.map[i][j].sb);
                }
                img1.setPixel(i,j,qRgb(r,g,b));
            }
        }
    }
    else
    {
        double scale=(showTimes*1.0-TotalTimes/3.0)/(TotalTimes*2/3.0);
        for(int i=0;i<w;i++)
        {
            for(int j=0;j<h;j++)
            {
                int x=(imgMap.map[i][j].ex-i)*scale+i;
                int y=(imgMap.map[i][j].ey-j)*scale+j;
                int r=(imgMap.map[i][j].er);
                int g=(imgMap.map[i][j].eg);
                int b=(imgMap.map[i][j].eb);
                img1.setPixel(x,y,qRgb(r,g,b));
            }
        }
    }
    showTimes--;
    showPic(img1.width(),img1.height());
}


void MainWindow::on_pushButton_clicked()
{

    timer1->stop();
    timer2->stop();
    timer3->stop();
    //设置定时器是否为单次触发。默认为 false 多次触发
    timer->setSingleShot(false);
    //启动或重启定时器, 并设置定时器时间：毫秒
    timer->start(30);
    //定时器触发信号槽

    showTimes=0;
}

void MainWindow::on_pushButton_2_clicked()
{
    timer->stop();
    timer2->stop();
    timer3->stop();
    timer1->setSingleShot(false);
    //启动或重启定时器, 并设置定时器时间：毫秒
    timer1->start(30);
    //定时器触发信号槽

    showTimes=TotalTimes;
}

void MainWindow::on_btn3_clicked()
{
    if(img1.width()>0&&img2.width()>0)
    {
        startReadMap=true;

        ui->btn3->setVisible(false);
        ui->progress1->setVisible(true);
        ui->btn4->setVisible(false);
        ui->btn5->setVisible(false);
        ui->btn6->setVisible(false);
        ui->btn7->setVisible(false);
        ui->info->setVisible(false);
        ui->imgLabel->setVisible(true);

        ui->progress1->setGeometry(ui->pushButton->x(),ui->pushButton->y(),ui->progress1->width(),ui->progress1->height());
        showPic(img1.width(),img1.height());

        img1_width=img1.width();
        img1_height=img1.height();

        img1=img1.scaled(resolution,resolution, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
        img2=img2.scaled(resolution,resolution, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

        imgMap.init(img1,img2);

        imgMap.start();
    }
}

void MainWindow::on_btn4_clicked()
{
    QString filename;
    filename = QFileDialog::getOpenFileName(this,"Choose Src File","","(*.*)");
    if(filename == "")
        return;
    else
    {
        img1=QImage(filename);
        if(img1.width()>10)
            ui->btn4->setText(filename);
        else
            ui->btn4->setText(tr("invalid file!"));
    }
}

void MainWindow::on_btn5_clicked()
{
    QString filename;
    filename = QFileDialog::getOpenFileName(this,"Choose Dst File","","(*.*)");
    if(filename == "")
        return;
    else
    {
        img2=QImage(filename);
        if(img2.width()>10)
            ui->btn5->setText(filename);
        else
            ui->btn5->setText(tr("invalid file!"));
    }
}

void MainWindow::on_btn6_clicked()
{
    resolution+=10;
    ui->info->setText(QString::number(resolution));
}

void MainWindow::on_btn7_clicked()
{
    resolution-=10;
    if(resolution<50)
        resolution=50;
    ui->info->setText(QString::number(resolution));
}

void MainWindow::on_pushButton_3_clicked()
{
    timer1->stop();
    timer->stop();
    timer3->stop();
    //设置定时器是否为单次触发。默认为 false 多次触发
    timer2->setSingleShot(false);
    //启动或重启定时器, 并设置定时器时间：毫秒
    timer2->start(30);
    //定时器触发信号槽

    showTimes=0;
}

void MainWindow::on_pushButton_4_clicked()
{
    timer->stop();
    timer2->stop();
    timer1->stop();
    timer3->setSingleShot(false);
    //启动或重启定时器, 并设置定时器时间：毫秒
    timer3->start(30);
    //定时器触发信号槽

    showTimes=TotalTimes;
}
